class DocumentsController < ApplicationController
  # GET /documents
  # GET /documents.xml
  before_filter :session_expiry
  before_filter :login_required
  
  private
  def set_positions_from_cookie
    position_strings = cookies[:positions].split(',')
	position_strings.each do |position_string|
	  position_data = position_string.split('_')
      new_position = DocumentsPosition.new
	  new_position.document_id = @document.id
	  new_position.product_id = position_data[0]
	  new_position.quantity = position_data[1]
	  @document.positions << new_position
	end
  end
  
  public
  def index
    @documents = Document.find(:all, :conditions => "document_type <> 'invoice'")

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @documents }
    end
  end

  # GET /documents/1
  # GET /documents/1.xml
  def show
    @document = Document.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @document }
    end
  end

  # GET /documents/new
  # GET /documents/new.xml
  def new
    @document = Document.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @document }
    end
  end

  # GET /documents/1/edit
  def edit
    @document = Document.find(params[:id])
  end

  # POST /documents
  # POST /documents.xml
  def create
    @document = Document.new(params[:document])
	@document.issue_date = Time.now
	
	set_positions_from_cookie
    
    respond_to do |format|
      if @document.save
        flash[:notice] = 'Document was successfully created.'
        format.html { redirect_to(@document) }
        format.xml  { render :xml => @document, :status => :created, :location => @document }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @document.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /documents/1
  # PUT /documents/1.xml
  def update
    @document = Document.find(params[:id])
	
	@document.positions = []
	set_positions_from_cookie

    respond_to do |format|
      if @document.update_attributes(params[:document])
        flash[:notice] = 'Document was successfully updated.'
        format.html { redirect_to(@document) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @document.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /documents/1
  # DELETE /documents/1.xml
  def destroy
    @document = Document.find(params[:id])
    @document.destroy

    respond_to do |format|
      format.html { redirect_to(documents_url) }
      format.xml  { head :ok }
    end
  end
  
  def findInteresting
    @query = ""
    @param_table = []
    
    @query = @query + "issue_date > ? "
    @param_table << prepare_from_date("issue_date")
    @query = @query + "AND issue_date < ? "
    @param_table << prepare_to_date("issue_date")
    
    @query = @query + "AND document_type <> 'invoice' AND document_type like ? "
    @param_table << "%"+params[:filter][:type]+"%"
	
	@query = @query + "AND number like ? "
    @param_table << "%"+params[:filter][:number]+"%"
    
    if params[:filter][:source] != ""
        @query = @query + "AND source_id = ? "
        @param_table << params[:filter][:source]
    end
    
    if params[:filter][:target] != "" 
        @query = @query + "AND target_id = ? "
        @param_table << params[:filter][:target]
    end
    
    @query = @query + "AND notes like ? "
    @param_table << "%"+params[:filter][:notes]+"%"
    
    if params[:filter][:creator] != ""
        @query = @query + "AND employee_id = ? "
        @param_table << params[:filter][:creator]
    end
    
    @documents = Document.find(:all, :conditions => [@query, *@param_table])
  end
end
